/*
 * -\-\-
 * Mobius
 * --
 * Copyright (c) 2017-2020 Spotify AB
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -/-/-
 */

package com.spotify.mobius.ohos;

import com.spotify.mobius.Connectable;
import com.spotify.mobius.Connection;
import com.spotify.mobius.ConnectionLimitExceededException;
import com.spotify.mobius.functions.Consumer;

import javax.annotation.Nonnull;

/**
 * MobiusLoopViewModelTestUtilClasses
 */
public class MobiusLoopViewModelTestUtilClasses {
    private MobiusLoopViewModelTestUtilClasses() {
    }

    /**
     * TestEvent
     */
    public static class TestEvent {
        final String name;

        TestEvent(String name) {
            this.name = name;
        }
    }

    /**
     * TestEffect
     */
    public static class TestEffect {
        final String name;

        TestEffect(String name) {
            this.name = name;
        }
    }

    /**
     * TestModel
     */
    public static class TestModel {
        final String name;

        public TestModel(String name) {
            this.name = name;
        }
    }

    /**
     * TestViewEffect
     */
    public static class TestViewEffect {
        final String name;

        TestViewEffect(String name) {
            this.name = name;
        }
    }

    /**
     * TestViewEffectHandler
     *
     * @param <E> e
     * @param <F> f
     * @param <V> v
     */
    public static class TestViewEffectHandler<E, F, V> implements Connectable<F, E> {
        final Consumer<V> viewEffectConsumer;
        private volatile Consumer<E> eventConsumer = null;

        public TestViewEffectHandler(Consumer<V> viewEffectConsumer) {
            this.viewEffectConsumer = viewEffectConsumer;
        }

        /**
         * sendEvent
         *
         * @param event event
         */
        public void sendEvent(E event) {
            eventConsumer.accept(event);
        }

        @Nonnull
        @Override
        public Connection<F> connect(Consumer<E> output) throws ConnectionLimitExceededException {
            if (eventConsumer != null) {
                throw new ConnectionLimitExceededException();
            }

            eventConsumer = output;

            return new Connection<F>() {
                @Override
                public void accept(F value) {
                    // do nothing
                }

                @Override
                public void dispose() {
                    // do nothing
                }
            };
        }
    }

    /**
     * An Effect Handler that simply sends a TestViewEffect to the given view effect consumer.
     */
    static class ViewEffectSendingEffectHandler implements Connectable<TestEffect, TestEvent> {
        private Consumer<TestViewEffect> viewEffectConsumer;

        public ViewEffectSendingEffectHandler(Consumer<TestViewEffect> viewEffectConsumer) {
            this.viewEffectConsumer = viewEffectConsumer;
        }

        @Nonnull
        @Override
        public Connection<TestEffect> connect(Consumer<TestEvent> output)
            throws ConnectionLimitExceededException {
            return new Connection<TestEffect>() {
                @Override
                public void accept(TestEffect value) {
                    viewEffectConsumer.accept(new TestViewEffect(value.name));
                }

                @Override
                public void dispose() {
                    // do nothing
                }
            };
        }
    }
}
